/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.comac.ins.isfd.mapper.IsfdMetadataMapper;
import com.comac.ins.isfd.service.IIsfdMetadataService;
import lombok.RequiredArgsConstructor;
import com.comac.ins.common.core.utils.MapstructUtils;
import com.comac.ins.common.core.utils.StringUtils;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.isfd.domain.IsfdMetadata;
import com.comac.ins.isfd.domain.bo.IsfdMetadataBo;
import com.comac.ins.isfd.domain.vo.IsfdMetadataVo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 元数据信息Service业务层处理
 *
 * @author Lion Li
 * @date 2024-06-14
 */
@RequiredArgsConstructor
@Service
public class IsfdMetadataServiceImpl implements IIsfdMetadataService {

    private final IsfdMetadataMapper baseMapper;

    /**
     * 查询元数据信息
     *
     * @param id 主键
     * @return 元数据信息
     */
    @Override
    public IsfdMetadataVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询元数据信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 元数据信息分页列表
     */
    @Override
    public TableDataInfo<IsfdMetadataVo> queryPageList(IsfdMetadataBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IsfdMetadata> lqw = buildQueryWrapper(bo);
        Page<IsfdMetadataVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的元数据信息列表
     *
     * @param bo 查询条件
     * @return 元数据信息列表
     */
    @Override
    public List<IsfdMetadataVo> queryList(IsfdMetadataBo bo) {
        LambdaQueryWrapper<IsfdMetadata> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IsfdMetadata> buildQueryWrapper(IsfdMetadataBo bo) {
        LambdaQueryWrapper<IsfdMetadata> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getDataSourceName()), IsfdMetadata::getDataSourceName, bo.getDataSourceName());
        lqw.eq(StringUtils.isNotBlank(bo.getDataSourceType()), IsfdMetadata::getDataSourceType, bo.getDataSourceType());
        lqw.like(StringUtils.isNotBlank(bo.getTableName()), IsfdMetadata::getTableName, bo.getTableName());
        lqw.eq(StringUtils.isNotBlank(bo.getTableDescription()), IsfdMetadata::getTableDescription, bo.getTableDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getSchemaInfo()), IsfdMetadata::getSchemaInfo, bo.getSchemaInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getRecordCount()), IsfdMetadata::getRecordCount, bo.getRecordCount());
        lqw.like(StringUtils.isNotBlank(bo.getOriginalFileName()), IsfdMetadata::getOriginalFileName, bo.getOriginalFileName());
        lqw.eq(bo.getOriginalFileId() != null, IsfdMetadata::getOriginalFileId, bo.getOriginalFileId());
        lqw.eq(bo.getParsingRuleId() != null, IsfdMetadata::getParsingRuleId, bo.getParsingRuleId());
        lqw.eq(StringUtils.isNotBlank(bo.getBusinessScenarioNo()), IsfdMetadata::getBusinessScenarioNo, bo.getBusinessScenarioNo());
        lqw.like(StringUtils.isNotBlank(bo.getBusinessScenarioName()), IsfdMetadata::getBusinessScenarioName, bo.getBusinessScenarioName());
        lqw.eq(StringUtils.isNotBlank(bo.getStorageType()), IsfdMetadata::getStorageType, bo.getStorageType());
        lqw.eq(StringUtils.isNotBlank(bo.getStorageLocations()), IsfdMetadata::getStorageLocations, bo.getStorageLocations());
        lqw.eq(StringUtils.isNotBlank(bo.getElementDataType()), IsfdMetadata::getElementDataType, bo.getElementDataType());
        lqw.like(StringUtils.isNotBlank(bo.getElementDataName()), IsfdMetadata::getElementDataName, bo.getElementDataName());
        lqw.eq(StringUtils.isNotBlank(bo.getElementLibrarySecondCategory()), IsfdMetadata::getElementLibrarySecondCategory, bo.getElementLibrarySecondCategory());
        lqw.eq(StringUtils.isNotBlank(bo.getCreatedBy()), IsfdMetadata::getCreatedBy, bo.getCreatedBy());
        lqw.eq(bo.getCreatedAt() != null, IsfdMetadata::getCreatedAt, bo.getCreatedAt());
        lqw.eq(StringUtils.isNotBlank(bo.getLastModifiedBy()), IsfdMetadata::getLastModifiedBy, bo.getLastModifiedBy());
        lqw.eq(bo.getLastModifiedAt() != null, IsfdMetadata::getLastModifiedAt, bo.getLastModifiedAt());
        lqw.eq(StringUtils.isNotBlank(bo.getDataOwner()), IsfdMetadata::getDataOwner, bo.getDataOwner());
        lqw.eq(StringUtils.isNotBlank(bo.getAccessPermissions()), IsfdMetadata::getAccessPermissions, bo.getAccessPermissions());
        lqw.eq(bo.getRetentionPeriod() != null, IsfdMetadata::getRetentionPeriod, bo.getRetentionPeriod());
        lqw.eq(StringUtils.isNotBlank(bo.getSecurityClassification()), IsfdMetadata::getSecurityClassification, bo.getSecurityClassification());
        lqw.eq(StringUtils.isNotBlank(bo.getTags()), IsfdMetadata::getTags, bo.getTags());
        lqw.eq(StringUtils.isNotBlank(bo.getRelatedSystems()), IsfdMetadata::getRelatedSystems, bo.getRelatedSystems());
        lqw.eq(StringUtils.isNotBlank(bo.getNotes()), IsfdMetadata::getNotes, bo.getNotes());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), IsfdMetadata::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getDataQualityMetrics()), IsfdMetadata::getDataQualityMetrics, bo.getDataQualityMetrics());
        return lqw;
    }

    /**
     * 新增元数据信息
     *
     * @param bo 元数据信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(IsfdMetadataBo bo) {
        IsfdMetadata add = MapstructUtils.convert(bo, IsfdMetadata.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改元数据信息
     *
     * @param bo 元数据信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(IsfdMetadataBo bo) {
        IsfdMetadata update = MapstructUtils.convert(bo, IsfdMetadata.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IsfdMetadata entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除元数据信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
